package com.aiou.web.controller;

import com.fasterxml.jackson.annotation.JsonView;
import com.aiou.dto.User;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author zyb
 */
@RestController
@RequestMapping(path = "/user")
public class UserController {
    //@GetMapping("/me")
    //public Object getCurrentUser(@AuthenticationPrincipal UserDetails user) {
    //    return user;
    //}

    @PostMapping
    public User create(@Valid @RequestBody User user, BindingResult errors) {

        if (errors.hasErrors()) {
            errors.getAllErrors().forEach(error -> System.out.println(error.getDefaultMessage()));
        }

        System.out.println(user.getId());
        System.out.println(user.getUsername());
        System.out.println(user.getPassword());
        System.out.println(user.getBirthday());

        user.setId("1");
        return user;
    }

    @PutMapping("/{id:\\d+}")
    public User update(@PathVariable int id, @Valid @RequestBody User user, BindingResult errors) {

        if (errors.hasErrors()) {
            errors.getAllErrors().forEach(error -> {
                System.out.println(error.getDefaultMessage());
            });
        }

        System.out.println(user.getId());
        System.out.println(user.getUsername());
        System.out.println(user.getPassword());
        System.out.println(user.getBirthday());

        user.setId("1");
        return user;
    }

    @DeleteMapping("/{id:\\d+}")
    public void delete(@PathVariable String id) {
        System.out.println(id);
    }
    //
    //@GetMapping
    //@JsonView(User.UserSimpleView.class)
    //public List<User> query(UserQueryCondition condition,
    //                        @PageableDefault(page = 2, size = 17, sort = "username,asc") Pageable pageable) {
    //
    //    System.out.println(ReflectionToStringBuilder.toString(condition, ToStringStyle.MULTI_LINE_STYLE));
    //
    //    System.out.println(pageable.getPageSize());
    //    System.out.println(pageable.getPageNumber());
    //    System.out.println(pageable.getSort());
    //
    //    List<User> users = new ArrayList<>();
    //    users.add(new User());
    //    users.add(new User());
    //    users.add(new User());
    //    return users;
    //}

    @GetMapping("/{id:\\d+}")
    @JsonView(User.UserDetailView.class)
    public User getInfo(@PathVariable String id) {
        User user = new User();
        user.setUsername("tom");
        return user;
    }
}
